package cn.hx.controller.front.item;

import cn.hx.entity.Item;
import cn.hx.entity.ItemGroup;
import cn.hx.entity.UserReport;
import cn.hx.service.ItemService;
import cn.hx.service.UserReportService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class ItemIndexController {
    @Resource
    private ItemService itemService;

    @Resource
    private UserReportService userReportService;

    @GetMapping({"index","/"})
    public String ItemIndex(ModelMap modelMap) {
        List<UserReport> userReports = userReportService.queryIndexReport();
        modelMap.addAttribute("dateByLimit",userReports);
        return "index";
    }

    @GetMapping("re_approve/item")
    public String fundItem(@RequestParam(defaultValue = "0") Integer groupId, ModelMap modelMap,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        int count = 0;
        if (groupId == 0) {
            count = itemService.count(new Item());
        } else {
            count = itemService.countByGroup(groupId);
        }
        Item item = new Item();
        ItemGroup itemGroup = new ItemGroup();
        itemGroup.setId(groupId);
        item.setItemGroup(itemGroup);
        List<Item> items = itemService.queryAllByLimit(item, (page - 1) * size, size);
        modelMap.put("list", items);
        modelMap.put("count", count);
        modelMap.put("page", page);
        modelMap.put("groupId", groupId);

        if (count==0){
            modelMap.put("pageSize",1);
        }else {
            modelMap.put("pageSize", (count % size) == 0 ? count / size : (count / size + 1));
        }
        return "re_approve/item";
    }
}
